/*
Copyright (C) 2008  Helmut Juskewycz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.juskewycz.dividere.core;

import com.juskewycz.dividere.action.AActionFactory;
import com.juskewycz.dividere.action.IProgressListener;
import com.juskewycz.dividere.action.IProgressObserverable;
import com.juskewycz.dividere.action.join.IJoinListener;
import com.juskewycz.dividere.action.join.IJoiner;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Controller to invoke a join process.
 * 
 * @author Helmut Juskewycz
 * @createdon 27. January 2008
 * @lastmodified 27. January 2008
 * @history
 * 27. January 2008 Finished version 1.0
 */
public class JoinController {

    private String filePath;
    private boolean deleteOriginal;
    private boolean overwriteFiles;

    /**
     * Creates a new <code>JoinController</code>. It also sets certain
     * parameters for the joining process.  So typically for every join a
     * <code>JoinController</code> has to be created.
     * 
     * @param filePath The path of the first file piece.
     * @param deleteOriginal Indicates whether the file pieces should be deleted
     * after the joining process.
     * @param overwriteFiles Indicates whether the existing joined file should
     * overwritten or not.
     */
    public JoinController(String filePath, boolean deleteOriginal, boolean overwriteFiles) {
	this.filePath = filePath;
	this.deleteOriginal = deleteOriginal;
	this.overwriteFiles = overwriteFiles;
    }
    
    /**
     * Starts joining the part files. If the <code>IJoiner</code>
     * implements <code>IProgressObserverable</code> the given listeners will
     * be added as observers to the joiner.
     * 
     * @param listeners Progress listeners which want to observe the joining
     * process.
     */
    public void join(List<IProgressListener> listeners) {

	IJoiner joiner = AActionFactory.getImpl().createJoiner();
	if (joiner instanceof IProgressObserverable) {
	    IProgressObserverable observerable = (IProgressObserverable) joiner;
	    for (IProgressListener iProgressListener : listeners) {
		observerable.addListener(iProgressListener);
	    }
	}

	if (deleteOriginal) {
	    joiner.addListener(new IJoinListener() {

		List<File> partFiles = new ArrayList<File>();

		public void partProcessBegin(File part) {

		}

		public void partProcessed(File part) {
		    partFiles.add(part);
		}

		public void fileJoined(File joinedFile) {
		    for (File file : partFiles) {
			file.delete();
		    }
		}

		public void fileJoinBegin(File joinedFile) {

		}
		});
	}

	if (overwriteFiles) {
	    joiner.addListener(new IJoinListener() {

		public void partProcessBegin(File part) {
		}

		public void partProcessed(File part) {

		}

		public void fileJoined(File joinedFile) {

		}

		public void fileJoinBegin(File joinedFile) {
		    joinedFile.delete();
		}
		});
	}

	joiner.join(filePath);
    }
}
